package com.karst.webnotes.chapter04
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.materials.utils.PrecisionMode;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	
	internal class MovieMaterialExample extends BasicView
	{
		public function MovieMaterialExample()
		{
			super() ;
			AsWingManager.getStage().frameRate = 12 ;
			createView() ;
			startRendering() ;
		}
		
		private var txt : TextField = null ;
		private var sprite : Sprite = null ;
		private var material : MovieMaterial = null ;
		private var plane : Plane = null ;
		private function createView() : void
		{
			if( sprite == null )
			{
				sprite = new Sprite() ;
				sprite.graphics.beginFill( 0x666666 ) ;
				sprite.graphics.drawRect( 0, 0, 200, 200 ) ;
				sprite.graphics.endFill() ;
			}

			if( txt == null )
			{
				txt = new TextField() ;
				txt.text = "Hello 3D world!";
				txt.autoSize = TextFieldAutoSize.LEFT;
				txt.setTextFormat(new TextFormat("Arial",25));
				
				txt.x = (sprite.width - txt.width) / 2;
				txt.y = (sprite.height - txt.height) / 2;
				sprite.addChild( txt ) ;
			}
			
			if( material == null )
			{
				material = new MovieMaterial( sprite ) ;
				material.smooth = true ;
				material.doubleSided = true ;
				material.precision = PrecisionMode.STABLE ;
			}
			
			if( plane == null )
				plane = new Plane( material, 400, 400, 2, 2 ) ;
			
			scene.addChild( plane ) ;
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick() ;
			if( plane )
				plane.localRotationY++ ;
		}
	}
}